package com.sugar.follow.up.module.business.service.publicityeducation;

import cn.hutool.core.collection.CollUtil;
import com.sugar.follow.up.module.business.controller.app.publicityeducation.vo.AppPublicityEducationDetailRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.follow.up.module.business.controller.admin.publicityeducation.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.publicityeducation.PublicityEducationDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.publicityeducation.PublicityEducationMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.*;

/**
 * 宣教 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class PublicityEducationServiceImpl implements PublicityEducationService {

    @Resource
    private PublicityEducationMapper publicityEducationMapper;

    @Override
    public Long createPublicityEducation(PublicityEducationSaveReqVO createReqVO) {
        // 插入
        PublicityEducationDO publicityEducation = BeanUtils.toBean(createReqVO, PublicityEducationDO.class);
        publicityEducationMapper.insert(publicityEducation);

        // 返回
        return publicityEducation.getId();
    }

    @Override
    public void updatePublicityEducation(PublicityEducationSaveReqVO updateReqVO) {
        // 校验存在
        validatePublicityEducationExists(updateReqVO.getId());
        // 更新
        PublicityEducationDO updateObj = BeanUtils.toBean(updateReqVO, PublicityEducationDO.class);
        publicityEducationMapper.updateById(updateObj);
    }

    @Override
    public void deletePublicityEducation(Long id) {
        // 校验存在
        validatePublicityEducationExists(id);
        // 删除
        publicityEducationMapper.deleteById(id);
    }

    @Override
        public void deletePublicityEducationListByIds(List<Long> ids) {
        // 删除
        publicityEducationMapper.deleteByIds(ids);
        }


    private void validatePublicityEducationExists(Long id) {
        if (publicityEducationMapper.selectById(id) == null) {
            throw exception(PUBLICITY_EDUCATION_NOT_EXISTS);
        }
    }

    @Override
    public PublicityEducationDO getPublicityEducation(Long id) {
        return publicityEducationMapper.selectById(id);
    }

    @Override
    public PageResult<PublicityEducationDO> getPublicityEducationPage(PublicityEducationPageReqVO pageReqVO) {
        return publicityEducationMapper.selectPage(pageReqVO);
    }

    @Override
    public AppPublicityEducationDetailRespVO appGetPublicityEducation(Long id) {
        PublicityEducationDO publicityEducationDO = publicityEducationMapper.selectById(id);

        if (Objects.nonNull(publicityEducationDO)) {
            publicityEducationMapper.update(PublicityEducationDO::getId, id, PublicityEducationDO::getLookCount, publicityEducationDO.getLookCount() + 1);
        }

        return BeanUtils.toBean(publicityEducationDO, AppPublicityEducationDetailRespVO.class);
    }

}